package com.zhangbo.study.designpatternlearning.observer;

import java.util.Observable;
import java.util.Observer;

/**
 * 观察者
 */
public class CurrentConditionsDisplay implements Observer, DisplayElement {

    private Observable observer;

    private float temperature;

    private float humidity;

    private String name;


    public CurrentConditionsDisplay(Observable observable,String name) {
        this.observer = observable;
        observable.addObserver(this);
        this.name = name;
    }

    @Override
    public void display() {
        System.out.println("name:" + name + "Current condition:" + temperature + "F derees and " + humidity + "% humidity");
    }

    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof WeatherData) {
            WeatherData weatherData = (WeatherData) o;
            this.temperature = weatherData.getTemperature();
            this.humidity = weatherData.getHumidity();
            display();
        }
    }
}
